package com.jindy.module.warehouseKeeper.service.impl;

import java.util.List;

import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.machine.domain.Machine;
import com.jindy.module.machine.mapper.MachineMapper;
import com.jindy.module.warehouse.domain.Warehouse;
import com.jindy.module.warehouse.mapper.WarehouseMapper;
import com.jindy.system.mapper.SysUserMapper;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.warehouseKeeper.mapper.WarehouseKeeperMapper;
import com.jindy.module.warehouseKeeper.domain.WarehouseKeeper;
import com.jindy.module.warehouseKeeper.service.IWarehouseKeeperService;

import javax.annotation.Resource;

/**
 * 仓库管理员Service业务层处理
 * 
 * @author jindy
 * @date 2023-11-03
 */
@Service
public class WarehouseKeeperServiceImpl implements IWarehouseKeeperService 
{
    @Resource
    private WarehouseKeeperMapper warehouseKeeperMapper;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private MachineMapper machineMapper;


    /**
     * 查询仓库管理员
     * 
     * @param id 仓库管理员主键
     * @return 仓库管理员
     */
    @Override
    public WarehouseKeeper selectWarehouseKeeperById(Long id)
    {
        return warehouseKeeperMapper.selectWarehouseKeeperById(id);
    }

    /**
     * 查询仓库管理员列表
     * 
     * @param warehouseKeeper 仓库管理员
     * @return 仓库管理员
     */
    @Override
    public List<WarehouseKeeper> selectWarehouseKeeperList(WarehouseKeeper warehouseKeeper)
    {
        return warehouseKeeperMapper.selectWarehouseKeeperList(warehouseKeeper);
    }



//    /**
//     * 新增仓库管理员
//     *
//     * @param warehouseKeeper 仓库管理员
//     * @return 结果
//     */
//    @Override
//    public int insertWarehouseKeeper(WarehouseKeeper warehouseKeeper)
//    {
//        return warehouseKeeperMapper.insertWarehouseKeeper(warehouseKeeper);
//    }

    /**
     * 新增仓库管理员
     *
     * @param warehouseKeeper 仓库管理员
     * @return 结果
     */
    @Override
    public int insertWarehouseKeeper(WarehouseKeeper warehouseKeeper)
    {
        //补全数据
        if(warehouseKeeper.getUserName() != null){
           warehouseKeeper.setUserId(SecurityUtils.getUserId());
        }
        if (warehouseKeeper.getWarehouseName() != null){
            Warehouse warehouse = warehouseMapper.selectWarehouseByName(warehouseKeeper.getWarehouseName());
            warehouseKeeper.setWarehouseId(warehouse.getId());
        }
        int result = warehouseKeeperMapper.insertWarehouseKeeper(warehouseKeeper);
        return result;
    }


    /**
     * 根据管理员修改机器仓库信息
     *
     * @param warehouseKeeper 仓库管理员
     * @return 结果
     */
    @Override
    public AjaxResult updateWarehouseForMachine(WarehouseKeeper warehouseKeeper){//传入machineNo
        //得到仓管员信息
        Long userId = SecurityUtils.getUserId();
        WarehouseKeeper warehouseKeeper1 = warehouseKeeperMapper.selectWarehouseKeeperByUserId(userId);

        //得到机器信息
        Machine machine = null;
        try {
            machine = machineMapper.selectMachineByMachineNo(warehouseKeeper.getMachineNo());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询机器信息失败: " + e.getMessage());
        }

        if(machine != null) {
            machine.setWarehouseId(warehouseKeeper1.getWarehouseId());
            machine.setWarehouseName(warehouseKeeper1.getWarehouseName());

            try {
                machineMapper.updateMachine(machine);
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("更新仓库失败: " + e.getMessage());
            }
        } else {
            return AjaxResult.error("该机器不存在");
        }

        return AjaxResult.success("更新仓库成功");
    }
//    public AjaxResult updateWarehouseForMachine(WarehouseKeeper warehouseKeeper){//传入machineNo
//        //得到仓管员信息
//        Long userId = SecurityUtils.getUserId();
//        WarehouseKeeper warehouseKeeper1 = warehouseKeeperMapper.selectWarehouseKeeperByUserId(userId);
//
//        //得到机器信息
//        Machine machine = machineMapper.selectMachineByMachineNo(warehouseKeeper.getMachineNo());
//        machine.setWarehouseId(warehouseKeeper1.getWarehouseId());
//        machine.setWarehouseName(warehouseKeeper1.getWarehouseName());
//
//        machineMapper.updateMachine(machine);
//        return AjaxResult.success("更新仓库成功");
//    }

    /**
     * 修改仓库管理员
     * 
     * @param warehouseKeeper 仓库管理员
     * @return 结果
     */
    @Override
    public int updateWarehouseKeeper(WarehouseKeeper warehouseKeeper)
    {
        return warehouseKeeperMapper.updateWarehouseKeeper(warehouseKeeper);
    }

    /**
     * 批量删除仓库管理员
     * 
     * @param ids 需要删除的仓库管理员主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseKeeperByIds(Long[] ids)
    {
        return warehouseKeeperMapper.deleteWarehouseKeeperByIds(ids);
    }

    /**
     * 删除仓库管理员信息
     * 
     * @param id 仓库管理员主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseKeeperById(Long id)
    {
        return warehouseKeeperMapper.deleteWarehouseKeeperById(id);
    }
}
